package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.bussness.domain.ExpAccount;
import cn.wolfcode.p2p.bussness.domain.ExpAccountFlow;
import cn.wolfcode.p2p.bussness.mapper.ExpAccountFlowMapper;
import cn.wolfcode.p2p.bussness.service.IExpAccountFlowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service@Transactional
public class ExpAccountFlowServiceImpl implements IExpAccountFlowService {

    @Autowired
    private ExpAccountFlowMapper expAccountFlowMapper;

    @Override
    public int save(ExpAccountFlow expAccountFlow) {
        return expAccountFlowMapper.insert(expAccountFlow);
    }

    @Override
    public int update(ExpAccountFlow expAccountFlow) {
        return expAccountFlowMapper.updateByPrimaryKey(expAccountFlow);
    }

    @Override
    public ExpAccountFlow get(Long id) {
        return expAccountFlowMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List<ExpAccountFlow> list = expAccountFlowMapper.selectList(qo);
        return new PageInfo(list);
    }

    /**
     * 体验标投标流水
     * @param expAccount    体验金账户
     * @param amount        改变金额
     */
    @Override
    public void createBidFlow(ExpAccount expAccount, BigDecimal amount) {
        createFlow(expAccount,amount,"体验标投标冻结" + amount + "元",Constants.EXPMONEY_FLOW_BID);
    }

    @Override
    public void createAudit2Flow(ExpAccount expAccount, BigDecimal amount) {
        createFlow(expAccount,amount,"体验标投标失败, 取消冻结" + amount + "元",Constants.EXPMONEY_FLOW_FALSE);
    }

    @Override
    public void createAuditSuccessFlow(ExpAccount expAccount, BigDecimal amount) {
        createFlow(expAccount,amount,"体验标投标成功: " + amount + "元",Constants.EXPMONEY_FLOW_SUCCESS);
    }

    @Override
    public void createExpAccountFlow(ExpAccount expAccount, BigDecimal amount) {
        createFlow(expAccount,amount,"成功收回体验金: " + amount + "元",Constants.EXPMONEY_REBACK_FLOW_SUCCESS);
    }

    /**
     * 记录流水公共模板
     * @param expAccount
     * @param amount
     * @param note
     * @param actionType
     */
    public void createFlow(ExpAccount expAccount, BigDecimal amount,String note, int actionType){
        ExpAccountFlow flow = new ExpAccountFlow();
        //交易类型
        flow.setActionType(actionType);
        //交易时间
        flow.setActionTime(new Date());
        //交易备注
        flow.setNote(note);
        //交易金额
        flow.setAmount(amount);
        //expAccountId 用户ID
        flow.setExpAccountId(expAccount.getId());
        //交易之后的体验金账户冻结金额
        flow.setFreezedAmount(expAccount.getFreezedAmount());
        //交易之后的体验金账户余额
        flow.setUsableAmount(expAccount.getUsableAmount());
        //保存到数据库
        this.save(flow);
    }
}
